$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var type = getUrlString('type');
    var appfirstload = getUrlString('appfirstload');
    var key = getUrlString('key');
    // var userid = getUrlString('userid');
    var fenlei = getUrlString('fenlei');
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0,sort= 0,pinlei = 0;
    $('.signInCancelText').click(function () {
        $('.goToSignIn').hide()
    });
    $('.signInConfirmText').click(function () {
        $('.goToSignIn').hide();
        window.location.href = 'dancool://fullweb='+urlView+'signIn/signIn.html?type=0'
    });

    function isSignIN () {
        getData('get', '/niu//apiv3/member_issignuptoday.php', {'markid':markid}, function (data) {
            console.log(data);
            if (data.data.issignuptoday === 0 && appfirstload === '1'){
                $('.goToSignIn').show()
            } else {
                $('.goToSignIn').hide()
            }
        })
    }

    isSignIN();


    //判断用户支付次数
    function member_userinfo () {
        getData('get', '/niu/apiv3/member_userinfo.php', {'markid':markid}, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let userid = data.data.id;
                if (getTimes('2021/09/16 00:00')){
                    $('#gotoqb').hide()
                }else{
                    pay_payinfo(userid);
                }

            }else {
                laymsg(data.msg)
            }

        });
    }
    member_userinfo();

    function getTimes (beginDateStr) {
        var curDate = new Date(),
            beginDate = new Date(beginDateStr);
        return curDate >= beginDate;

    }


    //判断用户支付次数
    function pay_payinfo (userid) {
        getData('get', '/niu/apiv3/pay_payinfo.php?type=paytimes', {'userid':userid}, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                if (data.data.paytimes === 0){
                    $('#gotoqb').show().click(function () {
                        window.location.href = 'dancool://paypage=';
                    });
                } else {
                    $('#gotoqb').hide()
                }

            }else {
                laymsg(data.msg)
            }

        });
    }

    //首页banner
    function bannerList() {

        getData('get', '/niu/apiv3/news_bannerlist.php', null, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                append += '<div class="swiper-slide gojs">' +
                    '                    <img src="../res/img/index/jsBanner.jpg" alt="">' +
                    '                </div>';
                // append += '<div class="swiper-slide goTaSmallActivity">' +
                //     '                    <img src="../res/img/index/activityBanner.jpg" alt="">' +
                //     '                </div>';
                $(data.data).each(function () {
                    append += '<div class="swiper-slide gotoDetail">' +
                        ' <input type="hidden" class="url" value="'+this.url+'"> '+
                        '                    <img src="'+this.pic+'" alt="">' +
                        '                    <div class="title">'+this.name+'</div>' +
                        '                </div>'
                });
                // append += '<div class="swiper-slide goTaSmallApp">' +
                //     '                    <img src="../res/img/index/gosmallapp.jpg" alt="">' +
                //     '                </div>';

                $('#bannerList').append(append);

                var myswiper = new Swiper('.swiper-container', {

                    spaceBetween: 30,
                    centeredSlides: true,
                    autoplay: {
                        delay: 1500,
                        disableOnInteraction: false,
                    },
                    loop:true,
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true,
                    },
                    // 如果需要前进后退按钮
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                });

                document.addEventListener('touchmove',(event)=>{
                    if (event.comesFromScrollable){
                        event.preventDefault();
                    }
                },{passive: false});
                $('.gojs').click(function () {
                    window.location.href = 'dancool://web='+urlView+'consignmentSales/consignment.html?type=0'
                });
                $('.goTaSmallApp').click(function () {
                    nativeCommand('dancool://wxsapp=gh_a16176d49b83|||pages/used/used')
                });
                $('.goTaSmallActivity').click(function () {
                    window.location.href = 'dancool://fullweb='+urlView+'danji/DWCupDanji.html?danjiid=14855'
                });
                $('#bannerList .gotoDetail').click(function () {
                    var url = $(this).find('.url').val();
                    if (url === ''){
                        window.location.href = urlView+'activity/help.html?markid='+markid;
                    }else {
                        window.location.href = url;
                    }
                });


                //二手详情
                $('#usedList li').click(function () {
                    var id = $(this).find('.id').val();
                    window.location.href = 'dancool://ershou='+id;
                });

            }else {
                laymsg(data.msg)
            }

        });
    }

    bannerList();

    $('#zxsb').click(function () {
        window.location.href = 'dancool://web='+urlView+'indexTypes.html?markid='+markid+'&type2=0'
    });

    function classify() {
        getData('get', '/niu/apiv3/niu_recommendlist.php', {'is_wx':1}, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                $.each(data.data,function () {
                    append += '<div class="menuTitle">' +
                        '                <span>'+this.key+'</span>' +
                        '                <span class="lookMore">查看更多></span>' +
                        '            </div>' +
                        '            <div class="menuContent">' +
                        '                <div class="swiper-container1 swiper-no-swiping">' +
                        '                    <div class="swiper-wrapper">' ;
                        $(this.niupool).each(function () {
                           append += '                        <div class="swiper-slide">' +
                               '                <input type="hidden" class="id" value="'+this.id+'">'+
                               '                <input type="hidden" class="is_parent" value="'+this.is_parent+'">'+
                               ' <div class="picdan">' +
                               '    <img class="pic" src="'+this.picdan+'" alt="">' ;
                           if (this.is_parent === '1'){
                               append += '    <div class="mores">多套</div>'
                           }
                            append += '</div> '+
                               '                            <div class="name">'+this.name+'</div>' +
                               '                            <div class="price">' +
                               '                                <span>'+this.showtag+'</span>' +
                               '                                <span>'+this.money2+'</span>' +
                               '                                <img src="../res/img/common/goldCoin.png" alt="">' +
                               '                            </div>' +
                               '                        </div>' ;
                        });
                        append +='                    </div>' +
                        '                </div>' +
                        '            </div>'
                });
                $('#danjiList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }

                var swiper = new Swiper('.swiper-container1', {
                    slidesPerView: 3,
                    spaceBetween: 10,
                    on:{
                        reachEnd: function(){

                        }
                    },
                });

                //蛋机列表
                $('#danjiList .lookMore').click(function () {
                    var key = $(this).prev().html();
                    var djtype;
                    // window.location.href = 'dancool://niulist=1;'+key+';'+key;
                    if (key === '主题限定'){
                        djtype = '0,6,8,9'
                    }else if (key === '一番赏'){
                        djtype = '7,8'
                    } else {
                        djtype = 1
                    }
                    window.location.href = 'dancool://web='+ urlView + 'indexType2.html?key='+key+'&djtype='+djtype
                });

                //蛋机详情
                $('#danjiList .swiper-slide').click(function () {
                    var id = $(this).find('.id').val();
                    var is_parent = $(this).find('.is_parent').val();
                    // var is_parent = $(this).find('.is_parent').val();
                    if (is_parent === '1'){
                        window.location.href = 'dancool://fullweb='+urlView+'danji/danji.html?danjiid='+id+'&markid='+markid+'&is_parent='+is_parent;
                    } else {
                        window.location.href = 'dancool://fullweb='+urlView+'danji/danji.html?danjiid='+id+'&markid='+markid+'&is_parent=3'
                    }
                });

            }else {
                laymsg(data.msg)
            }

        });
    }

    classify();

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                window.location.reload();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                danjiList(page)
            }
        }
    });

    function danjiList(page) {
        var data = {
            'markid':markid,
            'djtype':1,
            'key':'',
            'page':page,
            'djsort':'hot',
            'is_wx':1
        };
        getData('get', '/niu/apiv4/niu_getpoollist.php', data, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                $(data.data).each(function () {

                    append+='<li>' +
                        '<input type="hidden" class="id" value="'+this.id+'">'+
                        '                   <img class="picBanner" src="'+this.picbanner+'" alt="">' +
                        '                    <div class="name">'+this.name+'</div>' +
                        '                    <div class="price">' +
                        '                        <span>'+this.cuxiaotext+'</span>' +
                        '                        <div class="money">' +
                        '                           <img src="../res/img/common/goldCoin.png" alt=""> ' +
                        '                           <span>'+this.money2+'</span>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </li>' ;
                });
                $('#newBestList').append(append);


                totalSize = totalSize + 10;
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.endUpLoading(false);
                }

                $('#newBestList li').click(function () {
                    var id = $(this).find('.id').val();
                    window.location.href = 'dancool://fullweb='+urlView+'danji/danji.html?danjiid='+id+'&markid='+markid+'&is_parent=3'
                    // window.location.href = 'dancool://danji='+id;
                });

            }else {
                laymsg(data.msg)
            }

        });
    }



});

